<?php
namespace Admin\Controller;
use Think\Controller;
class NewController extends CommonController {
	//无限极分类  
	 public function lists() {    
        $Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
		$result = $Model->query("select * from kxs_new as a order by concat(a.p_id,a.id)");
		foreach($result as $k=>$v){	
			$count = substr_count($v['p_id'],",");
			$result[$k]['order']=$count;
			$result[$k]["NAME"] = $this->nbsp($count).$v['name'];
			//$this->assign("result",$result);
			//var_dump($v);
		}
		$this->assign("result",$result);

		//var_dump($result);  //为什么  知道了 存到$result里面
		//var_dump($result);
		
        $this->display();
    }
	//输出可用的空格函数
	public function nbsp($a){
		$str="";
		for($i=1;$i<$a;$i++){
				$str.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		} 
		return $str;
	}
	
	public function add(){
		//var_dump($uid);
		$Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
		$result = $Model->query("select * from kxs_new as a order by concat(a.p_id,a.id)");
		
		$this->assign("result",$result);

		$this->display();
		
	}
	public function addAction(){
		

		$_POST["create_time"] = date("Y-m-d H:i:s",time());
		$info = M("new")->add($_POST);
		 if($info){
			//成功
			$this->success("添加成功");
		}else{
			//失败
			$this->error("添加失败");
			
		}
		
		
	}
	public function del(){
		//取得要删除的id
		$id = $_GET["uid"];
		//$this->success($_GET["uid"]);
		$info = M("new")->where("id=$id")->select();
		//组合出上面数据的p_id
		$str = $info[0]['p_id'].$info[0]['id'].",";
		//查询是否有符合条件的数据
		$infozi = M("new")->where("p_id='$str'")->select();  //字符串为啥不加引号
		//判断有子集不能删除 如果为空则可以删除
		if(empty($infozi)){
			//成功
			$info = M("new")->where("id=$id")->delete();
			$this->success("删除成功");
		}else{
			//失败
			$this->error("有子新闻不能删除");
			
		}
	}
	public function edit(){
		$id = $_GET["uid"];
		$result = M("new")->where("id=$id")->find();//查询一条数据
		$this->assign("result",$result);
		$this->display();
		
	}
	public function editAction(){
		$id = $_POST["id"];
		$User = M("new"); // 实例化User对象
		$info = $User->where("id=$id")->save($_POST); // 根据条件更新记录
		if($info){
			//成功
			$this->success("修改成功");
		}else{
			//失败
			$this->error("修改失败");
			
		}
		
	}
}